<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8" %>
<%@ page import="com.alibaba.fastjson.JSONObject" %>
<%@ page import="okhttp3.OkHttpClient" %>
<%@ page import="okhttp3.Request" %>
<%@ page import="okhttp3.Response" %>
<%@ page import="org.apache.commons.codec.digest.DigestUtils" %>
<%@ page import="weaver.general.BaseBean" %>
<%@ page import="weaver.general.Util" %>
<%@ page import="weaver.hrm.HrmUserVarify" %>
<%@ page import="weaver.hrm.User" %>
<%@ page import="java.util.concurrent.TimeUnit" %>
<%
    /**
     * 秘钥组成： 自定义用户名+“.”+当前时间戳(到秒)+“.”+(当前时间戳（到秒）+自定义密码)的MD5码
     * 如： admin.1503930878.e67656eb651abfa4c3cfd2a8cbbe9cf1
     */
    BaseBean baseBean = new BaseBean();
    JSONObject jsonData = new JSONObject();
    User user = HrmUserVarify.getUser(request, response);
    String loginid = user.getLoginid();
    baseBean.writeLog(">>>>>anyEmailSSO.jsp>>>>loginid=" + loginid);
    String customUsername = "admin";
    String customPassword = "admin1234";
    String timestamp = getTimestamp();
    String encryContent = customUsername + "." + timestamp + "." + generateMD5(timestamp + customPassword);
    baseBean.writeLog(">>>>>anyEmailSSO.jsp>>>>encryContent=" + encryContent);
    String apiUrl = "http://192.168.1.13:80/restful/neworg.php/get_ext_authkey?passkey="
            + encryContent + "&key=" + loginid + "@any.com";
    baseBean.writeLog(">>>>>anyEmailSSO.jsp>>>>apiUrl=" + apiUrl);
    JSONObject resultJson = sendOKHttpGetRequest(apiUrl);
    baseBean.writeLog(">>>>>anyEmailSSO.jsp>>>>resultJson=" + resultJson);
    String message = Util.null2String(resultJson.getString("message"));
    String result = "";
    if ("success".equals(message)) {
        JSONObject resultDataJson = resultJson.getJSONObject("data");
        result = Util.null2String(resultDataJson.getString("result"));
    }
    jsonData.put("result", result);
    out.print(jsonData.toJSONString());
%>
<%!
    /**
     * 获取当前时间戳
     * @return
     */
    private String getTimestamp() {
        // 获取当前时间的毫秒时间戳
        long currentTimeMillis = System.currentTimeMillis();
        // 转换为秒时间戳
        long secondsSinceEpoch = currentTimeMillis / 1000;
        // 输出10位时间戳
        return String.format("%010d", secondsSinceEpoch);
    }

    // 使用Apache Commons Codec直接生成MD5哈希
    private String generateMD5(String input) {
        return DigestUtils.md5Hex(input);
    }

    /**
     * get请求
     *
     * @param apiUrl
     * @return
     */
    private JSONObject sendOKHttpGetRequest(String apiUrl) {
        JSONObject resultJson = new JSONObject();
        String message = "";
        String data = "";
        try {
            OkHttpClient client = new OkHttpClient().newBuilder()
                    .connectTimeout(60, TimeUnit.SECONDS)
                    .readTimeout(60, TimeUnit.SECONDS)
                    .writeTimeout(60, TimeUnit.SECONDS)
                    .build();
            Request request = new Request.Builder()
                    .url(apiUrl)
                    .get()
                    .build();
            Response response = client.newCall(request).execute();
            String result = Util.null2String(response.body().string());
            if (!"".equals(result)) {
                data = result;
                message = "success";
            } else {
                message = "response result is null";
            }
        } catch (Exception e) {
            message = "response timeout";
            e.printStackTrace();
        }
        resultJson.put("data", data);
        resultJson.put("message", message);
        return resultJson;
    }

%>